// import 'package:/model/home_model.dart';
import '';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;

import '../model/home_model.dart';

const HOME_URL = 'https://www.devio.org/io/flutter_app/json/home_page.json';

/*
*1.import 'package:http/http.dart' as http;  as 害怕重名和一个冲突  是一个别名
*
* */

/*
一个总结了flutter小知识点的网址
* https://blog.csdn.net/qq_39969226/article/details/94359864?ops_request_misc=&request_id=&biz_id=102&utm_term=utf8decoder&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-2-94359864.nonecase&spm=1018.2226.3001.4187
* */

/*
* future  ：Flutter中的Future是一个异步的特征  属于异编程  单线程+事件循环的设计模式
*
* */

class HomeDao {

  static Future<HomeModel> fetch() async {
    //类型转换
    var url = Uri.parse(HOME_URL);
    final response = await http.get(url);

    if (response.statusCode == 200) {
      //解决中文乱码的问题
      Utf8Decoder utf8decoder = const Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return HomeModel.fromJson(result);
    } else {
      throw Exception('data fail');
    }
  }
}
